package cn.com.cis.utils;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class DatabaseHelper {

	public static String getUrlUsingParams(String databaseType, String hostIp, int port, String databaseName, String addPara) {
		
		// 各种数据库jdbc URL样例
		// db2 (TODO additional params)
		// jdbc:db2://hostip:50000/database

		// sql server
		// jdbc:jtds:sqlserver://hostip:1433/database;

		// oracle with sid (TODO additional params)
		// jdbc:oracle:thin:@hostip:1521:sid

		// oracle with service name (TODO additional params)
		// jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=hostip)(port=1521))(connect_data=(service_name=serName)))

		// mongodb (TODO additional params)
		// mongodb://hostip:27000/database

		// derby (TODO additional params)
		// jdbc:derby://hostip:1527/databasePath
		
		String url = "";

		switch (databaseType) {
			case "DB2":
				url = "jdbc:db2://" + hostIp + ":" + port + "/" + databaseName;
				break;
			case "SQL_Server":
				url = "jdbc:sqlserver://" + hostIp + ":" + port + ";DatabaseName=" + databaseName;
				break;
			case "Oracle_SID":
				url = "jdbc:oracle:thin:@" + hostIp + ":" + port + ":" + databaseName;
				break;
			case "Oracle_Service_Name":
				url = "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=" + hostIp + ")(port=" + port + "))(connect_data=(service_name=" + databaseName + ")))";
				break;
			case "Mongo":
				url = "mongodb://" + hostIp + ":" + port + "/" + databaseName;
				break;
			case "Derby":
				url = "jdbc:derby://" + hostIp + ":" + port + "/" + databaseName;
				break;
		}

		return url;
	}
	
	public static boolean isConnectionAvaliable(String databaseType, String hostIp, int port, String databaseName, String username, String password){
		
		if("Mongo".equals(databaseType)){
			MongoClient mongoClient = new MongoClient(hostIp, port);
			MongoDatabase database = mongoClient.getDatabase(databaseName);
			
			// TODO 这里永远返回一个不为空的对象
			boolean pingResult = (null == database) ? false :true;
			
			if(null != mongoClient){
				mongoClient.close();
			}
			
			return pingResult;
		}
		
		String jdbcUrl = getUrlUsingParams(databaseType, hostIp, port, databaseName, "");
		
		String driver = "";
		switch (databaseType) {
			case "DB2":
				driver = "com.ibm.db2.jcc.DB2Driver";
				break;
				
			case "SQL_Server":
				driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
				break;
				
			case "Oracle_SID":
				driver = "oracle.jdbc.driver.OracleDriver";
				break;
				
			case "Oracle_Service_Name":
				driver = "oracle.jdbc.driver.OracleDriver";
				break;
				
			case "Derby":
				driver = "org.apache.derby.jdbc.ClientDriver";
				break;
		}
		
		java.sql.Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(jdbcUrl, username, password);
			
			if(null != conn){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (null != conn) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return false;
	}

}